package org.embedchat.protocol.message.client;

import org.embedchat.protocol.message.AbstractMessage;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Login message format:
 * <p/>
 * commandId - short
 * bodyLength - uint
 * roomId - uint
 * loginName - utf
 * password - utf
 *
 * @author koba
 */
public class LoginMessage extends AbstractMessage {

    private static final long serialVersionUID = -4669038285482916109L;
    private int roomId;    
    private String login;
    private String password;

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    @Override
    public String toString() {
        return "LoginMessage (login: "
                + login
                + ", password: "
                + password
                + ", roomId: "
                + roomId
                + ")";
    }

    @Override
    public int getMessageBodyLength() {
        return (login.getBytes().length + password.getBytes().length + 2);
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof LoginMessage))
            return false;
        LoginMessage castOther = (LoginMessage) other;
        EqualsBuilder equalsBuilder = new EqualsBuilder();
        equalsBuilder.append(login, castOther.login);
        equalsBuilder.append(password, castOther.password);
        equalsBuilder.append(roomId, castOther.roomId);
        return equalsBuilder.isEquals();
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
        hashCodeBuilder.append(login);
        hashCodeBuilder.append(password);
        hashCodeBuilder.append(roomId);
        return hashCodeBuilder.toHashCode();
    }

}
